package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.yuyou.fn.common.service.IBusFailLogService;
import com.yuyou.fn.settlement.service.IPayOrderService;
import com.yuyou.fn.settlement.vo.PreparePayResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by wens on 2017/3/6.
 */
@Controller
public class PayOrderController {

    private static final Logger log = LoggerFactory.getLogger(PayOrderController.class);

    @Resource
    private IPayOrderService payOrderService ;

    @Resource
    private IBusFailLogService busFailLogService;


    @RequestMapping("/api/pay/preparePay")
    @ResponseBody
    public ReturnResult<PreparePayResultVo> preparePay(@RequestParam("userId") String userId , @RequestParam("amount") BigDecimal amount , @RequestParam("busId") Long busId , @RequestParam("busType") String busType , @RequestParam("orderName") String orderName , @RequestParam("orderContent") String orderContent , @RequestParam("returnUrl") String returnUrl) {

        PreparePayResultVo s = payOrderService.preparePay(userId , busId, busType, amount, orderName , orderContent,returnUrl);
        return ReturnResult.success(s);
    }



    @RequestMapping("/api/pay/callback")
    @ResponseBody
    public JSONObject callback(@RequestParam(value = "id",required = false) String id,
                               @RequestParam(value = "version",required = false) String version,
                               @RequestParam(value = "appId",required = false) String appId,
                               @RequestParam(value = "orderId",required = false) Long orderId,
                               @RequestParam(value = "payOrderId",required = false) String payOrderId,
                               @RequestParam(value = "payStatus",required = false) int payStatus,
                               @RequestParam(value = "attach",required = false) String attach,
                               @RequestParam(value = "firstSucTrxId",required = false) String firstSucTrxId,
                               @RequestParam(value = "firstSucChannelId",required = false) String firstSucChannelId,
                               @RequestParam(value = "firstSucNotifyTime",required = false) long firstSucNotifyTime,
                               @RequestParam(value = "sign",required = false) String sign,
                               @RequestParam(value = "thirdTrxNo",required = false) String thirdTrxNo,
                               @RequestParam(value = "payPrx",required = false) String payPrx,
                               @RequestParam(value = "payMode",required = false) String payMode,
                               @RequestParam(value = "terminal",required = false) String terminal,
                               HttpServletResponse response ) {
        //System.out.println(body);
       // log.info("body:{},signature:{}",body,signature );
        try{
            payOrderService.callback(id,version,appId,orderId,payOrderId,payStatus,attach,firstSucTrxId,firstSucChannelId,firstSucNotifyTime,sign ,thirdTrxNo,payPrx,payMode,terminal);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("result","成功");
            jsonObject.put("id",id);
            return jsonObject;
        }catch (Exception e){
            log.error("callback fail!" , e );
            busFailLogService.log("pay_id" , id );
            String message="{\"error\": {\"code\":"+4000+", \"message\": \""+e.getMessage()+"\"}, \"id\": \""+payOrderId+"\"}";
            JSONObject jsonObject=JSONObject.parseObject(message,JSONObject.class);
            return jsonObject;
        }

    }

    /**
     * 调试时才使用这个接口
     * @param payOrderId
     * @return
     */
    @RequestMapping("/api/pay/send/paySuccessEvent")
    @ResponseBody
    public String sendPaySuccessEvent(@RequestParam("payOrderId") Long payOrderId ) {
        payOrderService.sendPaySuccessEvent(payOrderId);
        return "OK" ;
    }




}
